<?php

class PostComment extends CActiveRecord {
    /**
     * The followings are the available columns in table 'www_posts_comments':
     * @var integer $id
     * @var integer $post_id
     * @var integer $user_id
     * @var string $body
     * @var string $body_formatted
     * @var string $ip_address
     * @var integer $status
     * @var integer $created
     */

    const STATUS_PENDING = 0; // 待审核
    const STATUS_APPROVED = 1;  // 已批准

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{posts_comments}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
        return array(
            array('post_id, user_id, body, body_formatted, ip_address', 'required'),
            array('post_id, user_id, status, created', 'numerical', 'integerOnly'=>true),
            array('ip_address', 'length', 'max' => 20),
            array('body, body_formatted', 'length', 'min' => 10),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
        return array(
            // 归属 Post
            'ownerPost' => array(self::BELONGS_TO, 'Post', 'post_id'),
            // 归属 User
            'ownerUser' => array(self::BELONGS_TO, 'User', 'user_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
        'id' => 'Id',
        'post_id' => 'Post',
        'user_id' => Yii::t('postComment', 'user_id'),
        'body' => Yii::t('postComment', 'body'),
        'ip_address' => Yii::t('postComment', 'ip_address'),
        'status' => Yii::t('postComment', 'status'),
        'created' => Yii::t('postComment', 'created'),
        );
    }

    public function getUrl($post = null) {
        if ($post === null)
            $post = $this->post;
        return $post->url. '#c' . $this->id;
    }

    public function beforeValidate() {
        $this->user_id = ($this->user_id)
            ? Yii::app()->user->id : 0;
        $this->ip_address = CUtil::getIp();
        $parser = new CMarkdownParser();
        $this->body_formatted = $parser->safeTransform($this->body);

        return true;
    }

    public function behaviors() {
        return array(
            'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'created',
            )
        );
    }
}